package com.chen.tliaswebtest.service.impl;

import com.chen.tliaswebtest.exception.TliasException;
import com.chen.tliaswebtest.mapper.StudentMapper;
import com.chen.tliaswebtest.pojo.PageResult;
import com.chen.tliaswebtest.pojo.Student;
import com.chen.tliaswebtest.service.StudentService;
import com.chen.tliaswebtest.utils.EscapeUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author chen
 * @version 1.0
 * @create_date 2025/7/25
 * 学员服务实现类
 */
@Service
// spring事务默认只回滚运行时异常，编译时异常不会回滚
// 指定属性rollbackFor = {Exception.class}，可以捕获所有异常进行回滚
@Transactional(rollbackFor = {Exception.class})
public class StudentServiceImpl implements StudentService {
    private final StudentMapper studentMapper;

    public StudentServiceImpl(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    @Override
    public PageResult listByPage(String name, Integer degree, Integer clazzId,
                                 Integer page, Integer pageSize) {
        // 设置分页数
        PageMethod.startPage(page, pageSize);
        List<Student> students = studentMapper.listByPage(EscapeUtils.escapeLikePattern(name), degree, clazzId, page, pageSize);
        Page<Student> p = (Page<Student>) students;
        return PageResult.builder().total(p.getTotal()).rows(p.getResult()).build();
    }

    @Override
    public void removeByIds(List<Integer> ids) {
        studentMapper.deleteStudentByIds(ids);
    }

    @Override
    public void addStudent(Student student) {
        studentMapper.listAll().forEach(s -> {
            if (s.getNo().equals(student.getNo())) {
                throw new TliasException("学号已存在！");
            }
            if (s.getPhone().equals(student.getPhone())) {
                throw new TliasException("手机号码已存在！");
            }
            if (s.getIdCard().equals(student.getIdCard())) {
                throw new TliasException("身份证号已存在！");
            }
        });
        checkRequiredField(student);
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.addStudent(student);
    }

    @Override
    public Student getStudentById(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    @Override
    public void updateStudentViolation(Integer id, Integer score) {
        if (score <= 0) {
            throw new TliasException("违纪分数不能小于等于0!");
        }
        studentMapper.updateStudentViolation(id, score);
    }

    // 必须项目检查
    private void checkRequiredField(Student student) {
        if (student.getName() == null || student.getName().isEmpty()) {
            throw new TliasException("姓名不能为空！");
        }
        if (student.getNo() == null || student.getNo().isEmpty()) {
            throw new TliasException("学号不能为空！");
        }
        if (student.getGender() == null) {
            throw new TliasException("性别不能为空！");
        }
        if (student.getPhone() == null || student.getPhone().isEmpty()) {
            throw new TliasException("手机号不能为空！");
        }
        if (student.getIdCard() == null || student.getIdCard().isEmpty()) {
            throw new TliasException("身份证号不能为空！");
        }
        if (student.getIsCollege() == null) {
            throw new TliasException("是否为学院生不能为空！");
        }
        if (student.getClazzId() == null) {
            throw new TliasException("班级不能为空！");
        }
    }
}
